<?php
/**
 * 直播间点赞
 */

namespace app\common\cachemodel\live;


use think\Cache;
use think\Db;
use think\Log;

class LiveZan
{
    /**
     * 初始化直播弹幕表
     * @param string $tableName 表名
     * @return bool
     */
    public function initTable(string $tableName): bool
    {
        $cacheKey = 'table_exist:' . $tableName;
        $tableExist = Cache::get($cacheKey, 0);

        if ($tableExist)
        {
            return true;
        }

        $isTable = Db::query('select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`="live_data" and `TABLE_NAME`="' . $tableName . '"');
        if (!$isTable)
        { // 表不存在则创建
            try
            {
                $str = "CREATE TABLE `live_data`.`{$tableName}` (
                          `id` int(11) NOT NULL AUTO_INCREMENT,
                          `uid` int(11) NOT NULL DEFAULT 0 COMMENT '用户ID',
                          `live_id` int(11) NOT NULL DEFAULT 0 COMMENT '直播间ID',
                          `sign` tinyint(1) NOT NULL DEFAULT 0 COMMENT '点赞星座ID',
                          `create_time` bigint(15) NOT NULL DEFAULT 0 COMMENT '发送时间',
                          PRIMARY KEY (`id`),
                          KEY `uid` (`uid`)
                        )  ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='直播点赞信息'";
                // 创建数据库
                Db::execute($str);
            }
            catch (\Exception $e)
            {
                if (!strstr($e->getMessage(), 'already exists'))
                {
                    Log::write("直播间点赞表创建失败：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
                    return false;
                }
            }
        }

        Cache::set($cacheKey, 1, 86400);
        return true;
    }

    /**
     * 写入弹幕数据
     * @param int $liveRecordId
     * @param array $insertData
     */
    public function insertZan(int $liveRecordId, array $insertData): void
    {
        $tableName = 'live_zan_' . $liveRecordId;

        $initTable = $this->initTable($tableName);

        if ($initTable)
        {
            try
            {
                Db::table('`live_data`.' . $tableName)->insert($insertData);
            }
            catch (\Exception $e)
            {
                Log::write("点赞写入异常：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            }

            try
            {
                // 每分钟点赞总数新增
                (new LiveStatusDataRecord())->setMinuteInc($insertData['live_id'], 'zan_num', 1);
            }
            catch (\Exception $e)
            {
                Log::write("点赞缓存更新异常1：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            }
            try
            {
                // 用户热度数 自增
                (new LiveRecordUserHot())->userHotInc($insertData['uid'], $insertData['live_id'], $liveRecordId, 'zan_hot', 1);
            }
            catch (\Exception $e)
            {
                Log::write("点赞缓存更新异常2：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            }
            try
            {
                // 每日星座点赞 热度自增
                (new LiveSignHotDay())->setDaySignInc($insertData['live_id'], $liveRecordId, $insertData['sign'], 1);
            }
            catch (\Exception $e)
            {
                Log::write("点赞缓存更新异常3：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);
            }
        }
    }
}
